package com.open.gen.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.SQLException;

class DBResource {

    private static Logger log = LoggerFactory.getLogger(DBResource.class);

    // 获取数据库连接
    protected static Connection getConnection() throws Exception {

        String driverClassName = PropUtils.getProperties("dataSource.driverClassName");
        String url = PropUtils.getProperties("dataSource.url");
        String username = PropUtils.getProperties("dataSource.userName");
        String password = PropUtils.getProperties("dataSource.passWord");

        Class.forName(driverClassName);

        return java.sql.DriverManager.getConnection(url, username, password);
    }

    // 关闭数据库连接
    protected static void freeConnection(Connection conn) throws Exception {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            log.error("关闭数据库连接发生异常：", e);
        }
    }
}
